package com.jd.cmdb.tsc.util;

import com.alibaba.fastjson.JSON;
import com.jd.cmdb.tsc.entity.Task;
import com.jd.cmdb.tsc.entity.TaskLog;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by tianzhengguo on 2016/1/12.
 */
public final class TaskUtil {

    public static final String HOST_NAME = "hostName";
    public static final String START_TIME = "startTime";
    public static final String END_TIME = "endTime";
    public static final String DATE_FORMAT = "yyyyMMddHHmmss";
    public static final String ERROR_INFO = "errorInfo";
    public static final String TASK_STATUS = "status";
    public static final String ID = "id";
    public static final String TASK_NAME = "taskName";
    public static final String TASK_SUB_NAME = "subName";
    public static final String TASK_KEY = "task";
    public static final String LOG_KEY = "log";

//    public static Map<String, List<?>> parseJsonTask(String json) throws Exception {
//        Map<String, Object> jsonMap = JSON.parseObject(json, Map.class);
//
//        return parseTaskAndLogObject(jsonMap);
//    }

    public static Map<String, Object> parseTaskJson(String json) throws Exception{
        Map<String, String> jsonMap = JSON.parseObject(json, Map.class);
        Map<String,Object> retMap = parseTaskMap(jsonMap);
        retMap.put(TASK_STATUS,jsonMap.get(TASK_STATUS));
        return retMap;
    }

    public static Map<String, Object> parseTaskMap(Map<String,String> taskMap) throws Exception{
        String taskId = taskMap.get(ID);
        String hostName = taskMap.get(HOST_NAME);
        String startTime = taskMap.get(START_TIME);
        String endTime = taskMap.get(END_TIME);
        String errorInfo = taskMap.get(ERROR_INFO);
//        if(null != errorInfo && errorInfo.length() > 255)
//            errorInfo = errorInfo.substring(0,220);

        Map<String,Object> retMap = new HashMap<>();

        Task task = new Task();
        task.setId(taskId);
        task.setHostName(hostName);
//        task.setNextStartTime(null);
        retMap.put(TASK_KEY,task);

        TaskLog taskLog = new TaskLog();
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
        taskLog.setTaskId(taskId);
        taskLog.setId(UUID.randomUUID().toString());
        taskLog.setHostName(hostName);
        if(startTime == null)
            taskLog.setStartTime(null);
        else
            taskLog.setStartTime(new Timestamp(format.parse(startTime).getTime()));
        if(endTime == null)
            taskLog.setEndTime(null);
        else
            taskLog.setEndTime(new Timestamp(format.parse(endTime).getTime()));

        if("null".equals(errorInfo))
            errorInfo = null;
        taskLog.setErrorInfo(errorInfo);
        taskLog.setCreateTime(new Timestamp(System.currentTimeMillis()));
        retMap.put(LOG_KEY,taskLog);

        return retMap;
    }

//    public static String parseHostName(String json) {
//        Map<String, Object> jsonMap = JSON.parseObject(json, Map.class);
//        return parseHostName(jsonMap);
//    }
//
//    public static String parseHostName(Map<String, Object> jsonMap) {
//        Object hostName = jsonMap.remove(HOST_NAME);
//        if (hostName == null)
//            throw new RuntimeException("主机名为空.");
//        return hostName.toString();
//    }
//
//    public static Map<String, List<?>> parseTaskAndLogObject(Map<String, Object> jsonMap) throws Exception {
//        String hostName = parseHostName(jsonMap);
//
//        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
//
//        String key = null;
//
//        Timestamp startTime = null, endTime = null;
//        Iterator<String> it = jsonMap.keySet().iterator();
//
//        TaskLog log = null;
//        Task task = null;
//        List<Task> tasks = new ArrayList<Task>();
//        List<TaskLog> logs = new ArrayList<TaskLog>();
//
//        Map<String, List<?>> retMap = new HashMap<>();
//
//        Map<String, String> taskMap = null;
//
//        for (; it.hasNext(); ) {
//            key = it.next();
//            task = new Task();
//            log = new TaskLog();
//
//            log.setHostName(hostName);
//            taskMap = Map.class.cast(jsonMap.get(key));
//
//            task.setState(TaskService.STATE_FAIL);
//            task.setId(taskMap.get(ID));
//            tasks.add(task);
//            retMap.put(TASK_KEY,tasks);
//
//            log.setTaskId(taskMap.get(ID));
//            log.setStatus(Short.valueOf(taskMap.get(STATUS)));
//
//            startTime = new Timestamp(sdf.parse(taskMap.get(START_TIME).toString()).getTime());
//            log.setStartTime(startTime);
//
//            if (taskMap.get(END_TIME) == null)
//                endTime = null;
//            else
//                endTime = new Timestamp(sdf.parse(taskMap.get(END_TIME).toString()).getTime());
//            log.setEndTime(endTime);
//
//            log.setErrorInfo(taskMap.get(ERROR_INFO));
//
//            log.setCreateTime(new Timestamp(System.currentTimeMillis()));
//
//            logs.add(log);
//
//            retMap.put(LOG_KEY,logs);
//
//            it.remove();
//        }
//        return retMap;
//    }

    public static void main(String[] args) throws Exception {
//        Map<String, List<?>> logs = parseJsonTask("{\n" +
//                "    \"hostName\": \"worker_host\",\n" +
//                "    \"cap\": {\n" +
//                "        \"id\": \"1\",\n" +
//                "        \"startTime\": \"20160112100524\",\n" +
//                "        \"endTime\": \"20160112180524\",\n" +
//                "        \"errorInfo\": \"通过\",\n" +
//                "        \"status\": \"0\"\n" +
//                "    }\n" +
//                "}");
        System.out.println(new Timestamp(new SimpleDateFormat(DATE_FORMAT).parse("20160118134232").getTime()));
//        System.out.println(logs);
    }

}
